const path = require('path');

function resolve (dir) {
    return path.join(__dirname, dir);
}
function getDateTime() {
    let d, s;
    d = new Date();
    s = d.getFullYear(); // 取年份
    s += '' + ('0' + (d.getMonth() + 1)).substr(-2);// 取月份
    s += '' + ('0' + d.getDate()).substr(-2); // 取日期
    s += '' + ('0' + d.getHours()).substr(-2); // 取小时
    s += '' + ('0' + d.getMinutes()).substr(-2); // 取分
    // s += ':' + ('0' + d.getSeconds()).substr(-2); // 取秒
    return s;
}
const proxyApi = {
    'dev': 'http://res.dev.linpeng99.com',
    'test': 'http://res.test.linpeng99.com',
}[process.env.VUE_APP_TITLE] || 'http://res.dev.linpeng99.com';

process.env.VUE_APP_BUILD_TIME = getDateTime();
process.env.VUE_APP_VERSION = require('./package.json').version + '_' + getDateTime();
process.env.VUE_APP_BASE_DOMAIN = process.env.NODE_ENV === 'production' ? '/' : proxyApi + '/';

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
    productionSourceMap: false,
    outputDir: 'dist/' + process.env.VUE_LIB_VERSION,
    css: {
        extract: false,
        sourceMap: process.env.NODE_ENV !== 'production',
    },
    pages: {
        index: {
        // entry for the page
            entry: 'examples/beforeMain.js',
            // the source template
            template: 'public/index.html',
            // output as dist/index.html
            filename: 'index.html',
            // when using title option,
            // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
            title: 'linpengKeyBoard',
            // chunks to include on this page, by default includes
            // extracted common chunks and vendor chunks.
            // chunks: ['chunk-vendors', 'chunk-common', 'index'],
        },
        // when using the entry-only string format,
        // template is inferred to be `public/subpage.html`
        // and falls back to `public/index.html` if not found.
        // Output filename is inferred to be `subpage.html`.
        // subpage: 'src/subpage/main.js',
    },
    devServer: {
        proxy: {
            '/api': {
                target: proxyApi,
                changeOrigin: true,
            },
        },
    },
    chainWebpack: (config) => {
        config.resolve.alias
            .set('$root', resolve(''))
            .set('$public', resolve('public'))
            .set('$dist', resolve('dist'))
            .set('@assets', resolve('src/assets'))
            .set('@', resolve('src'));
        // 这里只写了两个个，你可以自己再加，按这种格式.set('', resolve(''))
    },
    configureWebpack: {
        output: {
            libraryExport: 'default',
            // filename: `[name].${process.env.VUE_APP_Version}.js`,
            // chunkFilename: `[name].${process.env.VUE_APP_Version}.js`,
        },
        externals: {
            'vue': 'Vue',
            'vuex': 'Vuex',
            'axios': 'axios',
        },
        // },
    //     plugins: [],
    },
};
